package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.BookDTO;
import com.example.mapper.BookMapper;
import com.example.pojo.Book;
import com.example.pojo.Category;
import com.example.pojo.PageBean;
import com.example.service.BookService;
import com.example.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    private final CategoryService categoryService;

    @Transactional
    public void insertWithCategory(Book book) {
        save(book);
        //查询新增图书的类别是否已经存在类别表中
        Category category = categoryService.findByName(book.getCategory());
        //如果该类别存在则更新其总数
        if (category != null) {
            Integer newSum = book.getSum() + category.getSum();
            categoryService.updateByName(category.getName(), newSum);
        } else {
            //创建一个新的图书类别并添加到类别表中
            Category c = new Category();
            c.setName(book.getCategory());
            c.setSum(book.getSum());
            categoryService.save(c);
        }
    }

    @Transactional
    public void deleteWithCategory(Integer id) {
        //查询想要删除的图书是否存在
        Book book = getById(id);
        if (book == null) {
            return;
        } else {
            //如果存在则删除并更新数量
            removeById(id);
            Category category = categoryService.findByName(book.getCategory());
            Integer newSum = category.getSum() - book.getSum();
            categoryService.updateByName(category.getName(), newSum);
        }
    }

    @Transactional
    public void updateWithCategory(Book book) {
        Book b = getById(book.getId());
        if (b == null) {
            return;
        }
        Integer newSum1 = 0, newSum2 = 0;
        //查询图书类别
        Category category1 = categoryService.findByName(b.getCategory());
        //不更新类别
        if (book.getCategory() == null) {
            //判断是否更新数量
            if (book.getSum() != null) {
                newSum1 = category1.getSum() + book.getSum() - b.getSum();
            }
            //更新数量
            categoryService.updateByName(b.getCategory(), newSum1);
            //更新类别
        } else {
            Category category2 = categoryService.findByName(book.getCategory());
            //修改后的类别不存在
            if (category2 == null) {
                //创建一个新的图书类别并添加到类别表中
                Category c = new Category();
                c.setName(book.getCategory());
                if (book.getSum() == null) {
                    c.setSum(b.getSum());
                } else {
                    c.setSum(book.getSum());
                }
                //将原来类别的数量减少
                newSum2 = category1.getSum() - b.getSum();
                categoryService.updateByName(b.getCategory(), newSum2);
                categoryService.save(c);
                //修改后的类别存在
            } else {
                //不更新数量
                if (book.getSum() == null) {
                    newSum1 = category1.getSum() - b.getSum();
                    newSum2 = category2.getSum() + b.getSum();
                    //更新数量
                } else {
                    newSum1 = category1.getSum() - b.getSum();
                    if (category1.getName().equals(category2.getName())) {
                        newSum2 = newSum1 + book.getSum();
                    } else {
                        newSum2 = category2.getSum() + book.getSum();
                    }
                }
                categoryService.updateByName(category1.getName(), newSum1);
                categoryService.updateByName(category2.getName(), newSum2);
            }
        }
        book.setUpdateTime(LocalDateTime.now());
        updateById(book);
    }

    @Override
    public PageBean<Book> find(BookDTO bookDTO) {
        //1.获取数据
        Integer id = bookDTO.getId();
        String name = bookDTO.getName();
        String category = bookDTO.getCategory();
        Integer page = bookDTO.getPage();
        Integer pageSize = bookDTO.getPageSize();

        //2.分页参数
        Page<Book> p = Page.of(page, pageSize);

        //3.排序条件
        p.addOrder(new OrderItem("update_time", false));

        //4.查询条件
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<Book>()
                .eq(id != null, Book::getId, id)
                .like(name != null && name != "", Book::getName, name)
                .like(category != null && category != "", Book::getCategory, category);

        //5.查询
        page(p, wrapper);

        //6.封装结果并返回
        return PageBean.of(p.getTotal(), p.getRecords());
    }
}
